#include "openmove_Prefix.h"
#include "SceneBase.h"
#include "OgreEngine.h"
#include "ExampleFrameListener.h"
#include "SkeletalAnimationFrameListener.h"
#include "OgreExternalTextureSourceManager.h"
#include "OgreExternalTextureSource.h"
#include "SceneFugana.h"
#include "SimpleFrameListener.h"

CSceneFugana :: CSceneFugana(COgreEngine &Creator, const String & Name, const int pos) : CSceneBase(Creator, Name, pos)
{
}

CSceneFugana :: ~CSceneFugana(void)
{
}

void CSceneFugana :: Create(TCreatorEntities const &CreatorEntities, TCreatorNodes const &CreatorNodes, TCreatorLights const &CreatorLights, ENetConfig IsClient, Vector3 &CameraStartPos)
{
	CSceneBase::Create(CreatorEntities, CreatorNodes, CreatorLights, IsClient, CameraStartPos);

	/*
	AddSpotlight("FuganaspotLight2", 
		Vector3(756.8786, 678.3102, 328.7922),
		-62.400055, 60.400051, 0, true
		);
	AddSpotlight("FuganaspotLight3", 
		Vector3(4.8541, 520.4237, 1231.4869),
		-30.000027, 44.800041, 0, true
		);
	AddSpotlight("FuganaspotLight4", 
		Vector3(-799.8776, 622.2128, -649.0336),
		-41.400036, 294.400269, 0, true
		);
		*/
}

void CSceneFugana :: removeParticleSystems(void)
{
	/*
	for (int i = 1; i <= 4; ++i)
	{
		m_pSceneNode->removeAndDestroyChild("Leaf" + StringConverter::toString(i));
		m_OgreEngine.m_pCurrentSceneManager->destroyParticleSystem("Leaf" + StringConverter::toString(i));
	}
	*/
}

void CSceneFugana :: addParticleSystems(void)
{
	/*
	ParticleSystem *system;

	system = m_OgreEngine.m_pCurrentSceneManager->createParticleSystem("Leaf1", "openmove/fugana");
	system->getEmitter(0)->setPosition(Vector3(-705.6, 96.4, 110.5));
	m_pSceneNode->createChildSceneNode("Leaf1")->attachObject(system);

	system = m_OgreEngine.m_pCurrentSceneManager->createParticleSystem("Leaf2", "openmove/fugana");
	system->getEmitter(0)->setPosition(Vector3(-598, 96.4, 54.4));
	m_pSceneNode->createChildSceneNode("Leaf2")->attachObject(system);

	system = m_OgreEngine.m_pCurrentSceneManager->createParticleSystem("Leaf3", "openmove/fugana");
	system->getEmitter(0)->setPosition(Vector3(735.519, 134.455, 830.221));
	m_pSceneNode->createChildSceneNode("Leaf3")->attachObject(system);

	system = m_OgreEngine.m_pCurrentSceneManager->createParticleSystem("Leaf4", "openmove/fugana");
	system->getEmitter(0)->setPosition(Vector3(187.374, 61, 522));
	m_pSceneNode->createChildSceneNode("Leaf4")->attachObject(system);
	*/
}

void CSceneFugana :: updateParticleSystems(void)
{
	/*
	for (int i = 1; i <= 4; ++i)
	{
		ParticleSystem *system = m_OgreEngine.m_pCurrentSceneManager->getParticleSystem("Leaf" + StringConverter::toString(i));
		ParticleAffector *affector = system->getAffector(1);
		affector->setParameter("force_vector", StringConverter::toString(m_WindForce));
	}
	*/
}

bool CSceneFugana :: frameStarted(const FrameEvent& evt)
{
	CSceneBase::frameStarted(evt);

	if (m_pCameraFrameListener->mInputDevice->isKeyDown(KC_Z) && m_pCameraFrameListener->mTimeUntilNextToggle <= 0)
	{
		fakeAddEvent(SOpenMoveDisplayPacket::e_FE_IncWind, 0);
	}
	else
	if (m_pCameraFrameListener->mInputDevice->isKeyDown(KC_X) && m_pCameraFrameListener->mTimeUntilNextToggle <= 0)
	{
		fakeAddEvent(SOpenMoveDisplayPacket::e_FE_DecWind, 0);
	}
	else
	if (m_pCameraFrameListener->mInputDevice->isKeyDown(KC_C) && m_pCameraFrameListener->mTimeUntilNextToggle <= 0)
	{
		fakeAddEvent(SOpenMoveDisplayPacket::e_FE_SetWind, 20, String("300 0 0"));
		m_pCameraFrameListener->mTimeUntilNextToggle = 1.0f;
	}
	else
	if (m_pCameraFrameListener->mInputDevice->isKeyDown(KC_V) && m_pCameraFrameListener->mTimeUntilNextToggle <= 0)
	{
		fakeAddEvent(SOpenMoveDisplayPacket::e_FE_SetWind, 20, String("0 -100 0"));
		m_pCameraFrameListener->mTimeUntilNextToggle = 1.0f;
	}

	updateParticleSystems();

	m_OgreEngine.setDebugText("WindSpeed: " + StringConverter::toString(m_WindForce));

	return (true);
}

void CSceneFugana :: processEvents(void)
{
	CSceneBase::processEvents();

	for (TEvents::iterator It = m_Events.begin(); It != m_Events.end(); ++It)
	{
		switch (It->m_Packet.eventid)
		{
			case SOpenMoveDisplayPacket::e_FE_IncWind:
			{
				++m_WindForce.x;
				break;
			}
			case SOpenMoveDisplayPacket::e_FE_DecWind:
			{
				--m_WindForce.x;
				break;
			}
			case SOpenMoveDisplayPacket::e_FE_SetWind:
			{
				Vector3 windSpeed = StringConverter::parseVector3(It->m_Packet.params);
				SimpleSpline spline;
				spline.addPoint(m_WindForce);
				spline.addPoint(windSpeed);
				Real time = It->m_pTimer->getSeconds() / It->m_Packet.duree;
				m_WindForce = spline.interpolate(time / 2.0f);

				m_OgreEngine.setDebugText("time: " + StringConverter::toString(time));
			}
			default:
			{
				break;
			}
		}
	}
}

void CSceneFugana :: AddSpotlight(String const &name, Vector3 const &position, Real const &xAngle, Real const &yAngle, Real const &zAngle, bool const CastShadow)
{
	SceneLight light;
	light.m_Name = name;
	light.setType(Light::LT_SPOTLIGHT);
	//light.setDiffuseColour(0.533000, 0.827132, 1.000000);
	//light.setDiffuseColour(0.255, 0.41, 0.5);
	light.setDiffuseColour(0.25, 0.25, 0.25);
	light.setCastShadows(CastShadow);
	light.setAttenuation(1000, 1, 0, 0);
	light.setSpotlightInnerAngle(Degree(63.959));
	light.setSpotlightFalloff(1);

	Vector3 vec = -Vector3::UNIT_Z;
	Quaternion rot = Quaternion(Degree(zAngle), Vector3(0, 0, 1)) * Quaternion(Degree(yAngle), Vector3(0, 1, 0)) * Quaternion(Degree(xAngle), Vector3(1, 0, 0));
	Matrix3 rotMat;
	rot.ToRotationMatrix(rotMat);
	vec = rotMat * vec;
	light.setDirection(vec);
	light.setPosition(position);

	//m_Lights.push_back(pLight);
	m_SceneLights.push_back(light);

	if (m_ShowEntitiesBBox)
	{
		Entity *entity = m_OgreEngine.m_pCurrentSceneManager->createEntity(name, "light.mesh");
		SceneNode *node = m_pSceneNode->createChildSceneNode(name);
		node->setPosition(light.getPosition());
		node->rotate(rot);
		node->scale(10, 10, 10);
		node->attachObject(entity);
	}
}

void CSceneFugana :: Detach(void)
{
	CSceneBase::Detach();
	removeParticleSystems();
}

void CSceneFugana :: Attach(void)
{
	CSceneBase::Attach();
	//m_pCurrentCamera->setPosition(-180.8f, 87.7f, 452.3f);
	//Quaternion Quat(Radian(Degree(0.0f)), Vector3(0, 1, 0));
	//m_pCurrentCamera->setOrientation(Quat);
	//m_pCurrentCamera->setDirection(0, 0, -1);
	//m_OgreEngine.m_pViewPort->setBackgroundColour(ColourValue(1.0f, 1.0f, 1.0f, 1.0f));

	m_WindForce = Vector3(0, -100, 0);

	//m_OgreEngine.m_pCurrentSceneManager->setSkyBox(true, "SkyBox/Test");

	addParticleSystems();
}